package com.nbjtjc.safe.service.emg.handle;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.emg.handle.*;
import com.nbjtjc.safe.model.emg.handle.*;
import com.nbjtjc.safe.model.sys.SysUser;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EmgHandlePublicService {

    @Autowired
    private EmgHandlePublicMapper emgHandlePublicMapper;

    @Autowired
    private EmgHandleMapper emgHandleMapper;
    @Autowired
    private EmgHandleListMapper emgHandleListMapper;

    @Autowired
    private EmgHandleListPublicMapper emgHandleListPublicMapper;

    @Autowired
    private EmgHandleGoodsMapper emgHandleGoodsMapper;
    @Autowired
    private EmgHandlePublicGoodsMapper emgHandlePublicGoodsMapper;

    @Autowired
    private EmgEventMapper emgEventMapper;
    public Result find(EmgHandlePublic emgHandlePublic) {
        Result result = new Result();
        PageHelper.startPage(emgHandlePublic.getPageCurrent(), emgHandlePublic.getPageSize());
        List<EmgHandlePublic> emgHandlePublics = emgHandlePublicMapper.search(emgHandlePublic);
        if (CollectionUtil.isNotEmpty(emgHandlePublics)) {
            result.setData(new PageInfo<>(emgHandlePublics));
        } else {
            result.setData(new PageInfo<>());
        }
        return result;
    }

    @Transactional(rollbackFor = Throwable.class)
    public Result delete(Integer id) {
        Result result = new Result();
        if (ObjectUtil.isNull(id) || id < 1) {
            result.setFalse("删除的id不能为空");
            return result;
        }
        emgHandlePublicMapper.deleteByPrimaryKey(id);
        return result;
    }

    @Transactional(rollbackFor = Throwable.class)
    public Result add(EmgHandlePublic emgHandlePublic) throws Exception {
        Result result = new Result();
        if (ObjectUtil.isNull(emgHandlePublic)
                || ObjectUtil.isNull(emgHandlePublic.getHandleId())) {
            result.setFalse("关键字段的信息不能为空");
            return result;
        }

        EmgHandle emgHandle = emgHandleMapper.selectByPrimaryKey(emgHandlePublic.getHandleId());
        if (ObjectUtil.isNull(emgHandlePublic.getName())) {
            emgHandlePublic.setName(emgHandle.getName());
        }
        emgHandlePublic.setTypeId(emgHandle.getTypeId());
        emgHandlePublic.setTopTypeId(emgHandle.getTopTypeId());
        emgHandlePublic.setDeptId(MyShiroUtils.getDeptId());
        int res = 0;
        emgHandlePublic.setAttentionItem(emgHandle.getRemark());
        emgHandlePublic.setCreateTime(new Date());
        res = emgHandlePublicMapper.insertSelective(emgHandlePublic);
        if (res == 0) {
            result.setFalse("新增失败");
        } else {
            //新增成功的时候插入物资和handleList
            initHandleList(emgHandlePublic.getHandleId(), emgHandlePublic.getId());
        }
//        updateEventStatus(emgHandlePublic);事件发布处置，不需要更新事件状态
        result.setData(emgHandlePublic);
        return result;
    }

    @Transactional(rollbackFor = Throwable.class)
    public Result update(EmgHandlePublic emgHandlePublic) {
        Result result = new Result();
        if (ObjectUtil.isNull(emgHandlePublic)
                || ObjectUtil.isNull(emgHandlePublic.getId())) {
            result.setFalse("关键字段的信息不能为空");
            return result;
        }
        int res = 0;
        res = emgHandlePublicMapper.updateByPrimaryKeySelective(emgHandlePublic);
        result.setData(emgHandlePublic);
        return result;
    }

    /**
     * 将事件的状态确认为已发布，即创建了对应的处理清单
     */
    private void updateEventStatus(EmgHandlePublic emgHandlePublic) throws Exception {
        EmgEvent emgEvent=emgEventMapper.selectByPrimaryKey(emgHandlePublic.getEventId());
        if(ObjectUtil.isNull(emgEvent)){
            return ;
        }
        emgEvent.setStatus(1);
        int res=emgEventMapper.updateByPrimaryKeySelective(emgEvent);
        if(res==0){
            throw new Exception("更新事件状态失败");
        }
    }
    public void initHandleList(int handleId, int handlePublicId) {
        EmgHandle emgHandle = emgHandleMapper.selectByPrimaryKey(handleId);
        if (ObjectUtil.isNull(emgHandle)) {
            return;
        }
        List<EmgHandleList> emgHandleLists = emgHandleListMapper.findByHandleId(handleId);
        List<EmgHandleListPublic> emgHandleListPublics = new ArrayList<>();
        if (CollectionUtil.isEmpty(emgHandleLists)) {
            return;
        }
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("user");
        emgHandleLists.forEach(emgHandleList -> {
            EmgHandleListPublic emgHandleListPublic = new EmgHandleListPublic(
                    handlePublicId, emgHandleList.getExecContent(), sysUser.getUserId(), 0
                    , emgHandleList.getHelpInfo(), emgHandleList.getId(), emgHandleList.getRespDept()
            );
            emgHandleListPublics.add(emgHandleListPublic);
        });
        emgHandleListPublicMapper.insertList(emgHandleListPublics);
        addHandlePublicGoods(handleId, handlePublicId);
    }

    public void addHandlePublicGoods(int handleId, int handlePublicId) {
        List<EmgHandleGoods> emgHandleGoodsList = emgHandleGoodsMapper.findByHandleId(handleId);
        if (CollectionUtil.isEmpty(emgHandleGoodsList)) {
            return;
        }
        List<EmgHandlePublicGoods> emgHandlePublicGoodsList = new ArrayList<>();
        emgHandleGoodsList.forEach(emgHandleGoods -> {
            EmgHandlePublicGoods emgHandlePublicGoods = new EmgHandlePublicGoods();
            emgHandlePublicGoods.setHandlePublicId(handlePublicId);
            emgHandlePublicGoods.setName(emgHandleGoods.getName());
            emgHandlePublicGoodsList.add(emgHandlePublicGoods);
        });
        if (CollectionUtil.isNotEmpty(emgHandlePublicGoodsList)) {
            emgHandlePublicGoodsMapper.insertList(emgHandlePublicGoodsList);
        }
    }
}
